package cn.xiongxf.mq.consumer;

import cn.xiongxf.service.IKillCourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * MQ事务消息【秒杀下单超时处理】消费者 - 秒杀服务
 */
@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "service-kill-consumer-kill-expire",
        topic = "topic-kill-expire",
        selectorExpression = "tags-kill-expire"
)
public class KillExpireForPayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IKillCourseService killCourseService;

    @Override
    public void onMessage(MessageExt message) {
        if(message.getBody() == null || message.getBody().length == 0) return;
        // 解析结果对象
        String preOrderKey = new String(message.getBody(), StandardCharsets.UTF_8);
        killCourseService.cancelPreOrder(preOrderKey);
    }
}
